<?php
//工具类，封装函数和算法等
class Tool {
    //获取客户端ip
    static public function getIP() {
        return $_SERVER['REMOTE_ADDR'];
    }
    static public function getDate() {
        return time();
    }
    //表单提交字符转义
	static public function setFormString($string) {
		if (!get_magic_quotes_gpc()) {
			if (Validate::isArray($string)) {
				foreach ($string as $key=>$value) {
					$string[$key] = self::setFormString($value);
				}
			} else {
				return addslashes($string); 
			}
		}
		return $string;
	}
    //表单选项转换
    static public function setFormItem($data,$key,$value) {
        $items = array();
        if(Validate::isArray($data)){
            foreach($data as $val){
                $items[$val->$key] = $val->$value;
            }
        }
        return $items;
    }
    //上一页地址
    static public function getPrevPage() {
        return empty($_SERVER['HTTP_REFERER']) ? '###' : $_SERVER['HTTP_REFERER'];
    }
    //显示html过滤
    static function setHtmlString($data) {
        if (Validate::isArray($data)) {
            if (Validate::isNullArray($data)) return $data;
            foreach ($data as $key=>$value) {
                $data[$key] = self::setHtmlString($value);  //递归
            }
        } elseif (is_object($data)) {
            foreach ($data as $key=>$value) {
                $data->$key = self::setHtmlString($value);  //递归
            }
        } else {
            $data = htmlspecialchars($data);
        }
        return $data;
    }
    /**
     * json 编码 解决 json_encode() 不支持中文的情况
     * 
     * @param array|object $data
     * @return array|object
     */
    public function ch_json_encode($data) {
        /**
         * 将中文编码
         * @param array $data
         * @return string
         */
        function ch_urlencode($data) {
            if (is_array($data) || is_object($data)) {
                foreach ($data as $k => $v) {
                    if (is_scalar($v)) {
                        if (is_array($data)) {
                            $data[$k] = urlencode($v);
                        } else if (is_object($data)) {
                            $data->$k = urlencode($v);
                        }
                    } else if (is_array($data)) {
                        $data[$k] = ch_urlencode($v); //递归调用该函数
                    } else if (is_object($data)) {
                        $data->$k = ch_urlencode($v);
                    }
                }
            }
            return $data;
        }
        $ret = ch_urlencode($data);
        $ret = json_encode($ret);
        return urldecode($ret);
    }
    //这是ob_gzip压缩函数    
    static public function ob_gzip ($content) {    
        if( !headers_sent()  && extension_loaded ("zlib")  && strstr ( $_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")){    
            $content = gzencode($content,9);    
            header ("Content- Encoding: gzip");    
            header ("Vary: Accept- Encoding");    
            header ("Content- Length: ".strlen ($content));    
        }    
    return ($content) ;    
    }
}